﻿namespace TanzimSaqib.NoBrainer
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web.UI;
    using System.Xml.Linq;

    public static class ExceptionExtensions
    {
        public static void InvalidOperation(this string message, Action action)
        {
            try
            {
                action();
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException(message, ex);
            } 
        }

        public static void Null(this string message, string text)
        {
            if(string.IsNullOrEmpty(text))
                throw new ArgumentNullException(message);
        }

        public static void Trace(this string message)
        {
            if(!string.IsNullOrWhiteSpace(message))
                System.Diagnostics.Trace.WriteLine(string.Format("NoBrainer >> {0}", message));
        }
    }

    internal static class ControlExtensions
    {
        public static IEnumerable<Control> All(this ControlCollection controls)
        {
            foreach (Control control in controls)
            {
                foreach (var childControl in control.Controls.All())
                    yield return childControl;

                yield return control;
            }
        }

        public static string InnerXml(this XElement rootNode)
        {
            return rootNode.Nodes().Aggregate(string.Empty, (element, node) => element += node.ToString());
        }
    }
}
